/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.codegen.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import me.ijleex.mgmt.codegen.entity.ColumnInfo;
import me.ijleex.mgmt.codegen.entity.TableInfo;
import me.ijleex.mgmt.codegen.service.ICodeGenService;
import me.ijleex.mgmt.framework.page.Page;
import me.ijleex.mgmt.framework.spring.ControllerSupport;

/**
 * 代码生成 URL
 *
 * @author liym
 * @since 2019-02-27 22:37 新建
 */
@RequestMapping("/code-gen")
@RestController
public class CodeGenAction extends ControllerSupport<TableInfo> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ICodeGenService service;

    public CodeGenAction(ICodeGenService service) {
        this.service = service;
    }

    /**
     * 获取当前连接的数据库中可用的表
     *
     * <p>http://localhost:8082/ij-mgmt/code-gen/tables?pageNo=1&pageSize=3&params[startTime]=20191001</p>
     *
     * @param request 请求
     * @param tableInfo 表信息
     * @return 列表
     * @since 2019-02-27 22:41 创建方法
     */
    @GetMapping("/tables")
    public Page<TableInfo> getTableList(HttpServletRequest request, TableInfo tableInfo) {
        this.setPageParams(request, tableInfo);
        List<TableInfo> list = this.service.getTableList(tableInfo);
        return this.wrapPageData(list, tableInfo);
    }

    /**
     * 根据表名查询列信息
     *
     * <p>http://localhost:8082/ij-mgmt/code-gen/table-columns?tableName=t_mgmt_user</p>
     *
     * @param request 请求
     * @param tableName 表名
     * @return 列信息
     * @since 2019-04-10 17:03
     */
    @GetMapping("/table-columns")
    public List<ColumnInfo> getTableColumns(HttpServletRequest request, String tableName) {
        return this.service.getTableColumns(tableName);
    }

    /**
     * 生成代码
     *
     * <p>http://localhost:8082/ij-mgmt/code-gen/generate?tableName=t_mgmt_user&tablePrefix=t_mgmt_&moduleName=system</p>
     *
     * @param request 请求
     * @param tableName 表名，批量生成时，用逗号分隔
     * @param tablePrefix 要删除的表名前缀，如 t_、t_mgmt_ 等
     * @param moduleName 模块包名
     * @param batch 是否批量生成：true/false
     * @return {@link ResponseEntity}
     * @date 2019-02-28 09:50 添加参数：batch
     * @date 2019-02-28 23:17 修改方法的返回参数类型为 ResponseEntity
     * @date 2019-03-04 11:27 添加参数：moduleName
     * @since 2019-02-27 23:16 创建方法
     */
    @GetMapping("/generate")
    public ResponseEntity<byte[]> generateCode(HttpServletRequest request,
            String tableName, String tablePrefix, String moduleName, Boolean batch) {
        this.logger.debug("generateCode tableName[{}], tablePrefix[{}], moduleName[{}], batch[{}]", tableName, tablePrefix, moduleName, batch);

        byte[] data;
        if (batch != null && batch) {
            data = this.service.generateCodeBatch(tableName, tablePrefix, moduleName);
        } else {
            data = this.service.generateCode(tableName, tablePrefix, moduleName);
        }
        String fileName = "codegen-result.zip";

        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<>(data, headers, HttpStatus.CREATED);
    }

}
